package com.car.action.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alipay.api.msg.Message;
import com.car.action.util.CommonController;
import com.car.common.model.MessageUnreadNum;
import com.car.common.model.UserFans;
import com.car.common.model.UserFriends;
import com.car.common.model.Users;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Db;
import com.utils.APIJSON;
import com.utils.StringUtil;
import com.utils.SystemClock;

public class FansController extends CommonController {
	
	public void follow() {
	 	String message = check_user(true);
		if (!StringUtil.empty(message)) {
			renderText(message);
		} else {
			if (StringUtil.empty(getPara("target_user_id"))) {
				renderText(APIJSON.result(4001));
				return;
			}
			int target_user_id = getParaToInt("target_user_id");
			Long user_id = super.getUserId();
			if (user_id.intValue() == target_user_id) {
				renderText(APIJSON.result(1051));
				return;
			}
			//判断是否关注
			UserFans one = UserFans.dao.findFirst("select id , user_id from user_fans where user_id = " + user_id+ " and target_user_id = " + target_user_id +" and delete_time is null ");
			if (one == null) {
				one = new UserFans();
				one.setUserId(user_id.intValue());
				one.setTargetUserId(target_user_id);
				one.setIsAuth(1);
				one.setStatus(1);
				one.setCreateTime(SystemClock.now()/1000L);
				one.setUpdateTime(SystemClock.now()/1000L);
				if (one.save()) {
					MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_FAN, target_user_id);
				}	
			} else {
				Db.delete("delete from user_fans where user_id = " + user_id+ " and target_user_id = " + target_user_id);
			}
			renderText(APIJSON.result(0));
		}
	}
	
	
    /**
     * 获取粉丝
     * @return array
     */
    public void my_fans(){
	   Long user_id = super.getUserId();
 	   int page = 1;
       if(!StringUtil.empty(getPara("page"))) {
     	  page = getParaToInt("page");
       }
       if(!StringUtil.empty(getPara("user_id"))) {
    	  // 自己查看
    	  if (user_id.longValue() == getParaToLong("user_id")) {
    		  MessageUnreadNum.dao.clearNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_FAN , user_id.intValue());
    	  }
      	  user_id = getParaToLong("user_id");
       } else {
    	   MessageUnreadNum.dao.clearNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_FAN, user_id.intValue());
       }
       List<UserFans> fansList = UserFans.dao.find("select a.user_id from user_fans as a left join users u on a.user_id = u.id where a.target_user_id = "+ user_id +" and u.nick_name is not null and length(u.nick_name) > 0 order by a.id desc limit " + (page -1) *15 +",15 " );
       ArrayList<Map<String,Object>> result = Lists.newArrayList();
       
       for(UserFans one : fansList) {
    	    Map<String,Object> temp = Maps.newHashMap();
    	   	String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
    	   	sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit from users as u left join area ae on ae.code = u.area_id where u.id = " + one.getUserId();
			
			Users user = Users.dao.findFirst(sql);
			if (user == null) {
				temp.put("user_data", Lists.newArrayList());
			} else {
				temp.put("user_data", Users.dao.info(user,null));
			}
			UserFans follow = UserFans.dao.findFirst("select id from user_fans where user_id = "+ one.get("user_id") +" and target_user_id = " + user_id + " order by id desc limit 1");
			temp.put("follow", follow != null ? 1: 2);
			
			UserFriends friends = UserFriends.dao.findFirst("select id from user_friends where user_id = "+ one.get("user_id") +" and target_user_id = " + user_id + " order by id desc limit 1");
			temp.put("friend", friends != null ? 1: 2);
			result.add(temp);
       }
       renderText(APIJSON.send(result));
    }
    
    /**
       * 获取我的关注
     * @return array
     */
    public void my_follow(){
	   Long user_id = super.getUserId();
 	   int page = 1;
       if(!StringUtil.empty(getPara("page"))) {
     	  page = getParaToInt("page");
       }
       if(!StringUtil.empty(getPara("user_id"))) {
      	  user_id = getParaToLong("user_id");
       }
       
       List<UserFans> followList = UserFans.dao.find("select target_user_id from user_fans where user_id = "+ user_id +" order by id desc limit " + (page -1) *15 +",15 " );
       ArrayList<Map<String,Object>> result = Lists.newArrayList();
       
       for(UserFans one : followList) {
    	    Map<String,Object> temp = Maps.newHashMap();
    	   	String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
    	   	sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit from users as u left join area ae on ae.code = u.area_id where u.id = " + one.getTargetUserId();
			
			Users user = Users.dao.findFirst(sql);
			if (user == null) {
				temp.put("user_data", Lists.newArrayList());
			} else {
				temp.put("user_data", Users.dao.info(user,null));
			}
			UserFans follow = UserFans.dao.findFirst("select id from user_fans where user_id = "+ user_id +" and target_user_id = " + one.getTargetUserId() + " order by id desc limit 1");
			temp.put("follow", follow != null ? 1: 2);
			
			UserFriends friends = UserFriends.dao.findFirst("select id from user_friends where user_id = "+ one.get("user_id") +" and target_user_id = " + user_id + " order by id desc limit 1");
			temp.put("friend", friends != null ? 1: 2);
			result.add(temp);
       }
       renderText(APIJSON.send(result));
    }
}
